package com.marketapp.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;

import com.marketapp.domain.Equity;
import com.marketapp.domain.Sector;
import com.marketapp.helper.DaoException;
import com.marketapp.util.StringUtil;

public class SectorDaoImpl implements ISectorDao {
	
	@Autowired
	SessionFactory sessionFactory;
	
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public Sector getSectorById(int sector_id) throws DaoException{
		Sector sector = new Sector();
		Session session = sessionFactory.openSession(); 
		try{
			sector = (Sector) session.get(Sector.class, sector_id);
		}catch(Exception ex){
			throw new DaoException("Error: Cannot retrieve data.", ex.getCause());
		}finally{
			session.close();
		}
		return sector;
	}

	@Override
	public List<Sector> getAllSector() throws DaoException {
		List<Sector> sectors = new ArrayList<Sector>();
		Session session = sessionFactory.openSession(); 
		try{
			Query query = session.getNamedQuery("getAllSector");
			sectors = query.list();
		}catch(Exception ex){
			throw new DaoException("Error: Cannot retrieve data.", ex.getCause());
		}finally{
			session.close();
		}
		return sectors;
	}

	@Override
	public void saveSector(Sector sector) throws DaoException {
		Session session = sessionFactory.openSession();
		Transaction transaction = session.getTransaction();
		try {
			transaction.begin();
			session.save(sector);
			transaction.commit();
		} catch (Exception ex) {
			throw new DaoException("Error: Sector cannot be saved "
					+ sector.getSectorName(), ex.getCause());
		} finally {
			session.close();
		}
		
	}

	public List<Sector> getSectorByName(String sector_name) throws DaoException {
		List<Sector> sectors = new ArrayList<Sector>();
		Session session = sessionFactory.openSession(); 
		try{
			Query query = session.getNamedQuery("getSectorByName").setString("sectorName", StringUtil.getStringUpperCase(sector_name).trim());
			sectors = query.list();
		}catch(Exception ex){
			throw new DaoException("Error: Cannot retrieve data.", ex.getCause());
		}finally{
			session.close();
		}
		return sectors;
	}

	@Override
	public List<Sector> searchSectorByName(String sector_name)
			throws DaoException {
		List<Sector> sectors = new ArrayList<Sector>();
		Session session = sessionFactory.openSession(); 
		try{
			Query query = session.getNamedQuery("searchSectorByName").setString("sectorName", "%"+StringUtil.getStringUpperCase(sector_name).trim()+"%");
			sectors = query.list();
		}catch(Exception ex){
			throw new DaoException("Error: Cannot retrieve data.", ex.getCause());
		}finally{
			session.close();
		}
		return sectors;
	}

	

}
